﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;

namespace EasyLib.Extensions
{
    public static class EnumerableExtensions
    {
        public static int IndexOf<T>(this IEnumerable<T> source, Predicate<T> predicate)
        {
            int i = 0;
            foreach (T item in source)
            {
                if (predicate(item))
                {
                    return i;
                }
                i++;
            }
            return -1;
        }

        public static void AddAll<T>(this ICollection<T> dest, ICollection<T> src)
        {
            if (dest == null || src == null || src.Count <= 0) return;
            foreach (T t in src)
            {
                dest.Add(t);
            }
        }

        /// <summary>        
        /// Extend each method        
        /// </summary>        
        public static void Each<T>(this IEnumerable<T> obj, Predicate<T> predicate)
        {
            foreach (T t in obj)
            {
                predicate(t);
            }
        }

        /// <summary>
        /// Join to string by separator
        /// </summary>
        public static string Join2Str(this IEnumerable obj, string separator)
        {
            if (obj == null) return null;
            StringBuilder sb = new StringBuilder();
            foreach (object o in obj)
            {
                if (sb.Length > 0)
                {
                    sb.Append(separator);
                }
                sb.Append(o);
            }
            return sb.ToString();
        }
    }
}
